package com.yd;

import com.yd.config.Config;
import com.yd.util.FileUtils;

import java.io.*;
import java.util.HashMap;
import java.util.Map;

/**
 * 根据键值和翻译的文本一一对应合成String.xml格式的文本，复制到Android项目中
 */
public class MergeStringClient {

    public static void main(String[] args) {

        File outFile = Config.outFile;
        mergeInfo(outFile);
    }


    /**
     * 合并翻译过后的文本为Android 字符串文件格式
     *
     * @param inputFile
     */
    public static void mergeInfo(File inputFile) {
        System.out.println("extractInfo>>>dir >>>>path = " + inputFile.getPath());
        if (inputFile != null && inputFile.isDirectory()) {
            File[] files = inputFile.listFiles();
            if (files == null || files.length == 0) {
                System.out.println("extractInfo>>>>>path = " + inputFile.getPath() + ">>>>为空目录");
            } else {
//                FileUtils.createOrExistsDir(outFile);
                mergeTranslatedText(obtainFileName(files), inputFile);
            }
        }
    }

    /**
     * 获取本目录下需要解析的文件名
     *
     * @param files
     * @return
     */
    public static Map<String, String> obtainFileName(File[] files) {
        Map<String, String> names = new HashMap<>();
        for (File file : files) {
            if (file.isFile()) {
                String name = file.getName().substring(0, file.getName().lastIndexOf("_"));
                String extension = FileUtils.getFileExtension(file);
                if (!names.containsKey(name)) {
                    System.out.println("合并的文件名>>>>>name = " + name);
                    names.put(name, extension);
                }
            } else if (file.isDirectory()) {
                mergeInfo(file);
            }
        }
        return names;
    }

    public static void mergeTranslatedText(Map<String, String> fileNames, File inputFile) {
        System.out.println("========================================");
        System.out.println("合并文件目录>>>>" + inputFile.getPath());
        for (String fileName : fileNames.keySet()) {
            String ext = fileNames.get(fileName);
            File mergeFile = new File(inputFile, fileName + "_merge_translate." + ext);
            //前一步提取出的key信息
            File inputKeyFile = new File(inputFile, fileName + "_key." + ext);
            //翻译后的文件
            File inputTxtFile = new File(inputFile, fileName + "_text." + ext);

            if (!inputKeyFile.exists() || !inputTxtFile.exists()) {
                System.err.print(inputKeyFile.getPath() + "\n或者\n" + inputTxtFile + "\n不存在！！");
            } else {
                FileUtils.createFileByDeleteOldFile(mergeFile);
                System.out.println("开始合并文件>>>>" + mergeFile.getPath());

                String strKey = null,strTxt = null;
                try (BufferedWriter writer = new BufferedWriter(new FileWriter(mergeFile));
                     BufferedReader readerKey = new BufferedReader((new FileReader(inputKeyFile)));
                     BufferedReader readerTrans = new BufferedReader(new FileReader(inputTxtFile))) {
                    String  startStr = "<resources xmlns:tools=\"http://schemas.android.com/tools\" tools:ignore=\"MissingTranslation\">";
                    String  endStr = "</resources>";
                    writer.write(startStr);
                    writer.newLine();
                    while ((strKey = readerKey.readLine()) != null&& (strTxt = readerTrans.readLine()) != null) {
                        strKey.trim();
                        if (strKey.replace(" ", "").equals("")) {
                            //空行
                            writer.write(strKey);
                            writer.newLine();
                        } else if (strKey.contains("<!--")||strKey.contains("-->")) {
                            writer.write(strKey);
                            writer.newLine();
                        } else if (strKey.length() > 0 && strTxt.length() > 0) {
                            String mergeLine = strKey + strTxt +"</string>";
                            mergeLine = mergeLine.replace("& amp;","&amp;").replace("\'","\\\'");
                            System.out.println("合并文件行>>>>mergeLine>>>" + mergeLine);
                            writer.write(mergeLine);
                            writer.newLine();
                        }
                    }
                    writer.write(endStr);
                    writer.flush();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }
    }
}
